import { createRouter, createWebHistory } from 'vue-router'
import Index from '../pages/index.vue'
import Aside from '../components/aside.vue'
import Login from '../login/login.vue'
import Admin from '../views/admin.vue'
import UserCenter from '../pages/userCenter.vue'
import Detail from '../pages/detailMess.vue'


// const pagefiles = import.meta.glob('../pages/*.vue'); // 前台页面目录
// const viewfiles = import.meta.glob('../views/*.vue'); // 后台页面目录

const routes = [
  {path:'/',redirect:'index'},
  {
    path: '/index',
    name: 'index',
    component: Index,
  },{
    path: '/detailMess',
    name: 'detailMess',
    component: Detail,
  },{
    path: '/aside',
    name: 'aside',
    component: Aside,
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },{
    path: '/options',
    name: 'options',
    component:()=>import('../pages/options.vue'),
  },{
    path: '/userCenter',
    name: 'userCenter',
    component: UserCenter,
    beforeEnter: (to, from, next) => {
      if (localStorage.getItem('userInfo') !=  undefined) {
        next();
      } else {
        // 在跳转到login页面的时候携带一个isLogin:false参数
        next({ path: '/login', query: { isLogin: false } }, { isLogin: false })
      }
    },
  },
  {
    path: '/admin',
    name: 'admin',
    component: Admin,
    children:[
      {path:'/admin/data',component:()=>import('../views/data.vue')},
      {path:'/admin/role',component:()=>import('../views/role.vue')},
      {path:'/admin/user',component:()=>import('../views/user.vue')},
      {path:'/admin/blackuser',component:()=>import('../views/blackuser.vue')},
      {path:'/admin/brand',component:()=>import('../views/brand.vue')},
      {path:'/admin/car',component:()=>import('../views/car.vue')},
      {path:'/admin/order',component:()=>import('../views/order.vue')},
      {path:'/admin/checking',component:()=>import('../views/checking.vue')},
    ],
    beforeEnter: (to, from, next) => {
      if (localStorage.getItem('userInfo') !=  undefined) {
        next();
      } else {
        // 在跳转到login页面的时候携带一个isLogin:false参数
        next({ path: '/login', query: { isLogin: false } }, { isLogin: false })
      }
    },
  }
]
const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router
